from deluser import Ui_Dialog
from PyQt5.QtWidgets import QDialog, QMessageBox
from PyQt5.QtCore import QTimer
import requests


class deluserWindow(Ui_Dialog, QDialog):
    def __init__(self, token, group_list, parent=None):
        super(deluserWindow, self).__init__(parent)
        self.setupUi(self)  # 创建界面内容
        self.access_token = token
        self.show_groupList(group_list)
        self.show_userList()
        # 当改变班级时，user_id列表也要跟着变化
        self.comboBox.currentIndexChanged.connect(self.show_userList)
        self.pushButton.clicked.connect(self.click_Yes)
        self.pushButton_2.clicked.connect(self.click_No)


    def click_Yes(self):
        group_id = self.comboBox.currentText()
        user_id = self.comboBox_2.currentText()
        if group_id == '' or user_id == '':
            QMessageBox.warning(self, "warning", "班级和学号不能为空！")
            return
        face_list = self.get_facelist(user_id, group_id)
        for list in face_list:
            flag = self.del_faceToken(group_id, user_id, list['face_token'])
            if flag == False:
                QMessageBox.critical(self, "DelUser", "删除用户失败！")
                self.close() # 返回为0
        self.accept()  # 返回为1

    def click_No(self):
        self.close()

    # 显示group_id列表
    def show_groupList(self, list):
        for i in list:
            self.comboBox.addItem(i)


    #显示user_id列表
    def show_userList(self):
        self.comboBox_2.clear()
        group_id = self.comboBox.currentText()
        user_list = self.user_list(group_id)
        for i in user_list:
            self.comboBox_2.addItem(i)


    # 获取user_id列表
    def user_list(self, group_id):
        request_url = "https://aip.baidubce.com/rest/2.0/face/v3/faceset/group/getusers"

        params = {
            "group_id": group_id
        }
        access_token = self.access_token
        request_url = request_url + "?access_token=" + access_token
        headers = {'content-type': 'application/json'}
        response = requests.post(request_url, data=params, headers=headers)
        if response:
            message = response.json()
            if message['error_code'] == 0:
                return message['result']['user_id_list']
            else:
                return []


    # 获取人脸列表
    def get_facelist(self, user_id, group_id):
        request_url = "https://aip.baidubce.com/rest/2.0/face/v3/faceset/face/getlist"

        params = {
            "user_id": user_id,
            "group_id": group_id
        }
        access_token = self.access_token
        request_url = request_url + "?access_token=" + access_token
        headers = {'content-type': 'application/json'}
        response = requests.post(request_url, data=params, headers=headers)
        if response:
            message = response.json()
            if message['error_code'] == 0:
                return message['result']['face_list']
            else:
                return []


    # 删除人脸信息
    def del_faceToken(self, group_id, user_id, face_token):
        request_url = "https://aip.baidubce.com/rest/2.0/face/v3/faceset/face/delete"

        params = {
            "user_id": user_id,
            "group_id": group_id,
            "face_token": face_token
        }
        access_token = self.access_token
        request_url = request_url + "?access_token=" + access_token
        headers = {'content-type': 'application/json'}
        response = requests.post(request_url, data=params, headers=headers)
        if response:
            message = response.json()
            if message['error_code'] == 0:
                return True
            return False
